package com.five.community.controller;

import com.five.community.bean.*;
import com.five.community.bean.vo.ChargeprojectVo;
import com.five.community.bean.vo.ComplaintVo;
import com.five.community.bean.vo.PersonVo;
import com.five.community.contant.AppContants;
import com.five.community.service.ChargedetailsmanagerService;
import com.five.community.service.ChargeprojectService;
import com.five.community.service.HousingService;
import com.five.community.service.VillageService;
import com.five.community.util.FileUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/chargeproject")
public class ChargeprojectController {
    @Autowired
    private ChargeprojectService chargeprojectService;
    @Autowired
    private VillageService villageService;
    @Autowired
    private ChargedetailsmanagerService chargedetailsmanagerService;

    //显示收费项目一览画面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map,Integer pageNum,String pname) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pname == null || pname == "") {
            try {
                PageInfo<ChargeprojectVo> pageInfo = chargeprojectService.getPageInfo(pageNum, AppContants.chargepageSize);
                map.addAttribute("pageInfo", pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                PageHelper.startPage(pageNum, AppContants.chargepageSize);
                List<ChargeprojectVo> chargeprojectVos = chargeprojectService.selectByName(pname);
                PageInfo<ChargeprojectVo> pageInfo = new PageInfo<>(chargeprojectVos);
                map.addAttribute("pageInfo", pageInfo);
                Chargeproject chargeproject = new Chargeproject();
                chargeproject.setPname(pname);
                map.addAttribute("chargeproject", chargeproject);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "chargeproject_list";//返回出一个所有收费项目的画面
    }
    //显示添加收费项目画面
    @RequestMapping("/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages", villages);
            List<Chargedetailsmanager> chargedetailsmanagers = chargedetailsmanagerService.selectAll();
            map.addAttribute("chargedetailsmanagers", chargedetailsmanagers);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "chargeproject_add";//返回一个添加收费项目的画面
    }

    //添加一条收费项目信息方法
    @RequestMapping("/add")
    public String add(ChargeprojectVo chargeprojectVo, MultipartFile photos) {
        int id1 = Integer.parseInt(chargeprojectVo.getVname());
        int id2 = Integer.parseInt(chargeprojectVo.getTname());
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建唯一的文件名
        chargeprojectVo.setEvidence(fileName);
        chargeprojectVo.setCreateTime(new Date());
        try {
            Chargeproject chargeproject = new Chargeproject(
                    chargeprojectVo.getId(),
                    chargeprojectVo.getPservicecode(),
                    chargeprojectVo.getPname(),
                    new Date(),
                    fileName,
                    id1,
                    id2);
            chargeprojectService.insert(chargeproject);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "chargeproject_add";//返回一个添加收费项目的画面
    }

    //删除方法
    @RequestMapping("/delete")
    public String delete(int id) {
        try {
            chargeprojectService.deleteById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/chargeproject/listUi";
    }

    //显示修改收费项目画面
    @RequestMapping("/editUi")
    public String updateUi(int id,ModelMap map) {
        try {
            Chargeproject chargeproject = chargeprojectService.selectById(id);
            System.out.println("chargeproject.getId() = " + chargeproject.getId());
            List<Village> villages = villageService.selectAll();
            map.addAttribute("chargeproject",chargeproject);
            map.addAttribute("villages", villages);
            List<Chargedetailsmanager> chargedetailsmanagers = chargedetailsmanagerService.selectAll();
            map.addAttribute("chargedetailsmanagers", chargedetailsmanagers);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "chargeproject_edit";//返回一个添加收费项目的画面
    }


    //修改方法
    @RequestMapping("/edit")
    public String update(ChargeprojectVo chargeprojectVo, MultipartFile photos) {
        int id3 = Integer.parseInt(chargeprojectVo.getVname());
        int id4 = Integer.parseInt(chargeprojectVo.getTname());
        System.out.println("chargeprojectVo.getId() = " + chargeprojectVo.getId());
       //4
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(photos);
        } catch (IOException e) {
            e.printStackTrace();
        }
         chargeprojectVo.setEvidence(fileName);
        //
        chargeprojectVo.setCreateTime(new Date());
        try {
            Chargeproject chargeproject = new Chargeproject(
                    chargeprojectVo.getId(),
                    chargeprojectVo.getPservicecode(),
                    chargeprojectVo.getPname(),
                    new Date(),
                    fileName,
                    id3,
                    id4
            );
            chargeprojectService.update(chargeproject);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "chargeproject_edit";//返回一个添加收费项目的画面
    }

    //姓名查询
    @RequestMapping("/select")
    public String selecet(String name,ModelMap map){
        if(name == ""){
            return "redirect:/chargeproject/listUi";
        }
        else{
            try {
                List<ChargeprojectVo> chargeprojectVos = chargeprojectService.selectByName(name);
                map.addAttribute("chargeprojectVos", chargeprojectVos);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return "chargeproject_list";
        }
    }
}